/****

    activequant - activestocks.eu

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

	
	contact  : contact@activestocks.eu
    homepage : http://www.activestocks.eu

****/
package org.activequant.math.regression;

import org.activequant.core.domainmodel.SeriesSpecification;
import org.activequant.core.domainmodel.Symbol;
import org.activequant.core.domainmodel.data.CandleSeries;
import org.activequant.core.types.Ohlc;
import org.activequant.data.retrieval.ICandleSeriesSource;


/**
 * @TODO desc<br>
 * Holds the following associated variables:
 * <ul>
 * <li>seriesSource(ICandleSeriesSource)</li>
 * <li>ohlc(Ohlc) enum</li>
 * </ul>
 * <b>History:</b><br>
 *  - [11.08.2007] Created (Erik Nijkamp)<br>
 *
 *  @author Erik Nijkamp
 */
public class SeriesAdapter {
	/**
	 * private ICandleSeriesSource seriesSource;
	 */
	private ICandleSeriesSource seriesSource;
	/**
	 * private Ohlc ohlc;
	 */
	private Ohlc ohlc;
	/**
	 * constructs a SeriesAdapter using the given seriesSource(ICandleSeriesSource) and ohlc(Ohlc) enum to set its 
	 * associated seriesSource(ICandleSeriesSource) and ohlc(Ohlc)
	 * @param seriesSource
	 * @param ohlc
	 */
	public SeriesAdapter(ICandleSeriesSource seriesSource, Ohlc ohlc) {
		this.seriesSource = seriesSource;
		this.ohlc = ohlc;
	}
	/**
	 * <strong>1.</strong> Fetches candles(CandleSeries) for the given spec(SeriesSpecification) using the associated seriesSource(ICandleSeriesSource).<br/>
	 * <strong>2.</strong> Creates and returns a Series object representation of that candles(CandleSeries) looking at its prices according to the 
	 * associated ohlc(Ohlc) enum (open, high, low or close price).
	 * @param spec
	 * @return
	 * @throws Exception
	 */
	public Series read(SeriesSpecification spec) throws Exception {
		CandleSeries candles = seriesSource.fetch(spec);
		Series series = SeriesUtil.convertToSeries(candles, ohlc);
		return series;
	}
	/**
	 * <strong>1.</strong> Creates candles(CandleSeries[]) by fetching candle series for each of the given specs(SeriesSpecification...) using the associated seriesSource(ICandleSeriesSource).<br/>
	 * <strong>2.</strong> Creates series(Series[]) array with a Series object for each of those candles(CandleSeries[]) looking at their prices according to the 
	 * associated ohlc(Ohlc) enum (open, high, low or close price).<br/>
	 * <strong>3.</strong> Creates and returns a Group using that series(Series[]) to set its associated series(Vector&lt;Series&gt;)
	 * @param specs
	 * @return
	 * @throws Exception
	 */
	public Group read(SeriesSpecification ... specs) throws Exception {
		CandleSeries[] candles = new CandleSeries[specs.length];
		for(int i = 0; i < specs.length; i++) {
			candles[i] = seriesSource.fetch(specs[i]);
		}		
		Series[] series = SeriesUtil.convertToSeries(candles, ohlc);
		return new Group(series);
	}
	/**
	 * <strong>1.</strong> Fetches candles(CandleSeries) for the given symbol(Symbol) using the associated seriesSource(ICandleSeriesSource).<br/>
	 * <strong>2.</strong> Creates and returns a Series object representation of that candles(CandleSeries) looking at its prices according to the 
	 * associated ohlc(Ohlc) enum (open, high, low or close price).
	 * @param symbol
	 * @return
	 * @throws Exception
	 */
	public Series read(Symbol symbol) throws Exception {
		CandleSeries candles = seriesSource.fetch(new SeriesSpecification(symbol));
		Series series = SeriesUtil.convertToSeries(candles, ohlc);
		return series;
	}
	/**
	 * <strong>1.</strong> Creates candles(CandleSeries[]) by fetching candle series for each of the given symbols(Symbol...) using the associated seriesSource(ICandleSeriesSource).<br/>
	 * <strong>2.</strong> Creates series(Series[]) array with a Series object for each of those candles(CandleSeries[]) looking at their prices according to the 
	 * associated ohlc(Ohlc) enum (open, high, low or close price).<br/>
	 * <strong>3.</strong> Creates and returns a Group using that series(Series[]) to set its associated series(Vector&lt;Series&gt;)
	 * @param symbols
	 * @return
	 * @throws Exception
	 */
	public Group read(Symbol... symbols) throws Exception {
		CandleSeries[] candles = new CandleSeries[symbols.length];
		for(int i = 0; i < symbols.length; i++) {
			candles[i] = seriesSource.fetch(new SeriesSpecification(symbols[i]));
		}		
		Series[] series = SeriesUtil.convertToSeries(candles, ohlc);
		return new Group(series);
	}
}
